package com.vq3.digitalizationsoftwareintegrates.Utils.ImageUtil;

import com.vq3.digitalizationsoftwareintegrates.Entity.Enum.ImageType;
import com.vq3.digitalizationsoftwareintegrates.Entity.Enum.StatusType;
import com.vq3.digitalizationsoftwareintegrates.Entity.Spot_Check.MongoDBEntity.MongoDB_S_C_Point_ContentEntity;
import com.vq3.digitalizationsoftwareintegrates.Entity.Enum.S_CType;
import com.vq3.digitalizationsoftwareintegrates.Entity.Spot_Check.S_C_Point_ContentEntity;
import com.vq3.digitalizationsoftwareintegrates.Entity.User.UserEntity;
import com.vq3.digitalizationsoftwareintegrates.MongoDB.Spot_Check.MongoDB_S_C_Point_ContentDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Import;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Import({ImageUtil.class})
public class OnOrDownLoadImage {
    @Autowired
    ImageUtil imageUtil;

    final MongoDB_S_C_Point_ContentDao mongoDB_s_c_point_contentDao;

    public OnOrDownLoadImage(MongoDB_S_C_Point_ContentDao mongoDB_s_c_point_contentDao) {
        this.mongoDB_s_c_point_contentDao = mongoDB_s_c_point_contentDao;
    }
    /**点检信息在这里存*/
    public List<S_C_Point_ContentEntity> tempOnLoadImage(List<S_C_Point_ContentEntity> s_c_point_contentEntities_f, String path, HttpServletRequest httpServletRequest) throws IOException {
        ImageUtil imageUtil = new ImageUtil();
        List<MongoDB_S_C_Point_ContentEntity> s_c_point_contentEntities_Mongo = new ArrayList<>();
        UserEntity userEntity = (UserEntity)httpServletRequest.getSession().getAttribute("USER");
        String point_name = (String)httpServletRequest.getSession().getAttribute("POINT_NAME");
        try {


        for (S_C_Point_ContentEntity s_c_point_contentEntity : s_c_point_contentEntities_f) {
            MongoDB_S_C_Point_ContentEntity s_c_point_contentEntity_Mongo = new MongoDB_S_C_Point_ContentEntity();
            MultipartFile point_content_standardImage = s_c_point_contentEntity.getPoint_Content_standardImage();
            try {
                s_c_point_contentEntity_Mongo.setPoint_Content_Id(s_c_point_contentEntity.getPointContentId());
                s_c_point_contentEntity_Mongo.setS_C_Type(S_CType.TEMP);
                s_c_point_contentEntity_Mongo.setItemName(point_name);
                s_c_point_contentEntity_Mongo.setStatus_Type(StatusType.NotSubmitted);
            }catch (NullPointerException e){
                System.out.println("id为空");
            }
            try {
                s_c_point_contentEntity_Mongo.setPoint_Content(s_c_point_contentEntity.getPointContent());
                s_c_point_contentEntity_Mongo.setSc_fun(s_c_point_contentEntity.getScFun());
                s_c_point_contentEntity_Mongo.setSc_benchmark(s_c_point_contentEntity.getScBenchmark());
            }catch (NullPointerException e){
                System.out.println("项目为空");
            }
            try {
                s_c_point_contentEntity_Mongo.setUserName(userEntity.getUname());

            }catch (NullPointerException e){
                System.out.println("用户名为空");
            }
            try {
                s_c_point_contentEntity_Mongo.setItemName(point_name);
            }catch (NullPointerException e){
                System.out.println("点位名为空");
            }


            if (point_content_standardImage != null) {
                Map<String, String> paramMap = imageUtil.saveImage(point_content_standardImage, path);
                String filePath = paramMap.get("filePath");
                String uuid = paramMap.get("uuid");
                s_c_point_contentEntity.setImageName(uuid);
                s_c_point_contentEntity_Mongo.setImageName(uuid);
                s_c_point_contentEntity_Mongo.setImagePath(filePath);
            }
            s_c_point_contentEntities_Mongo.add(s_c_point_contentEntity_Mongo);
        }
        }catch (NullPointerException e) {
            System.out.println("1111");
        }
        mongoDB_s_c_point_contentDao.saveTempImg(s_c_point_contentEntities_Mongo);

        return s_c_point_contentEntities_f;
    }
    public List<S_C_Point_ContentEntity> downLoadImage( List<MongoDB_S_C_Point_ContentEntity> s_c_point_contentEntityList) throws IOException {

        List<S_C_Point_ContentEntity> s_c_point_contentEntities = new ArrayList<>();

        for (MongoDB_S_C_Point_ContentEntity mongoDB_s_c_point_contentEntity : s_c_point_contentEntityList) {
            S_C_Point_ContentEntity s_c_point_contentEntity = new S_C_Point_ContentEntity();
            s_c_point_contentEntity.setPointContentId(mongoDB_s_c_point_contentEntity.getPoint_Content_Id());
            s_c_point_contentEntity.setPointContent(mongoDB_s_c_point_contentEntity.getPoint_Content());
            s_c_point_contentEntity.setScFun(mongoDB_s_c_point_contentEntity.getSc_fun());
            s_c_point_contentEntity.setScBenchmark(mongoDB_s_c_point_contentEntity.getSc_benchmark());
            s_c_point_contentEntity.setPointName(mongoDB_s_c_point_contentEntity.getItemName());
            s_c_point_contentEntity.setImageName(mongoDB_s_c_point_contentEntity.getImageName() +"." +"jpeg");
            s_c_point_contentEntities.add(s_c_point_contentEntity);
        }
        return s_c_point_contentEntities;

    }

    public void deleteTempImage(List<MongoDB_S_C_Point_ContentEntity> mongoDB_s_c_point_contentEntities) {
        for (MongoDB_S_C_Point_ContentEntity mongoDB_s_c_point_contentEntity : mongoDB_s_c_point_contentEntities) {
            String imageName = mongoDB_s_c_point_contentEntity.getImageName();
            String imagePath = mongoDB_s_c_point_contentEntity.getImagePath();
            boolean b = imageUtil.deleteImage(imagePath + File.separator + imageName + "." + ImageType.JPEG);
            System.out.println(b);

        }

    }
}
